/**
 * 第三方登录工具函数
 */
import { ElMessage } from 'element-plus'
import { getAuthorizeUrl } from "@/api/system/login"
import { navigateTo } from '#app'

// 判断设备类型
const  getDeviceType = () => {
  return navigator.userAgent.includes('Mobile') ? 'mobile' : 'pc';
};
// 弹窗配置
const windowFeatures = 'width=1000,height=600,left=400,top=200';
/**
 * 打开第三方登录窗口
 */
export const openThirdPartyLogin = async (platform) => {
  const res = await getAuthorizeUrl(platform)
  if(res.code == 200){
    const authorizeUrl = res.data+`&display=${getDeviceType()}`
    
    // 移动端直接在当前窗口打开
    if (getDeviceType() === 'mobile') {
      navigateTo(authorizeUrl, { external: true });
      return null;
    } else {
      // PC端保持原有行为，打开新窗口
      return window.open(authorizeUrl, '_blank', windowFeatures);
    }
  }else{
    ElMessage.error(res.message);
    return null;
  }
};

/**
 * QQ登录快捷方法
 */
export const openQqLogin = (callbackPath = '/callback') => {
  return openThirdPartyLogin('qq', callbackPath);
};

/**
 * 微博登录快捷方法
 */
export const openWeiboLogin = (callbackPath = '/callback') => {
  return openThirdPartyLogin('weibo', callbackPath);
};

/**
 * Gitee登录快捷方法
 */
export const openGiteeLogin = (callbackPath = '/callback') => {
  return openThirdPartyLogin('gitee', callbackPath);
};
